package mblog.web.controller.api;

import com.taobao.api.ApiException;
import mblog.core.data.GoodsKeyword;
import mblog.core.persist.service.GoodsKeywordService;
import mblog.web.taobao.Goods;
import mblog.web.taobao.GoodsDetail;
import mblog.web.taobao.TaoBaoService;
import mblog.web.taobao.TaoBaoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/api")
public class GoodsController {

    @Autowired
    private TaoBaoService taoBaoService;

    @Autowired
    private GoodsKeywordService goodsKeywordService;


    @ResponseBody
    @RequestMapping("/goods_detail.json")
    public GoodsDetail findGoodsDetail(String goodId) {
        try {
            return taoBaoService.getGoodsDetail(goodId);
        } catch (IOException | ApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    @ResponseBody
    @RequestMapping("/goods_type_page.json")
    public List<Goods> findGoodsTypePage(String type, Integer page) {
        try {
            return taoBaoService.findPageGoods(type, page);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @ResponseBody
    @RequestMapping("/goods_type_cid_page.json")
    public List<Goods> findGoodsTypeCidPage(String type, String cid, Integer page, String px) {
        try {
            return taoBaoService.findPageGoodsByCid(type, cid, px, page);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @ResponseBody
    @RequestMapping("/find_top100.json")
    public List<Goods> findTop100() {
        return taoBaoService.findTop100();
    }


    @ResponseBody
    @RequestMapping("/find_swipe_goods.json")
    public List<Goods> findSwipeGoods() throws IOException {
        return taoBaoService.findSwipeGoods();
    }


    @ResponseBody
    @RequestMapping("/goods_search.json")
    public List<Goods> goodsSearch(String keyword, Integer page, String px) throws IOException {
        return taoBaoService.findGoodsSearch(keyword, page, px);
    }

    @ResponseBody
    @RequestMapping("/keywords.json")
    public List<GoodsKeyword> keywords() {
        return goodsKeywordService.keywords();
    }

}
